{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fa88dac1",
   "metadata": {},
   "source": [
    "# 人脸识别实践——宿舍智能识别门禁系统（xxx栋） \n",
    "* 1.创建FaceSet\n",
    "* 2.查询FaceSet\n",
    "* 3.存入face_token数据\n",
    "* 4.获取FaceSet信息数据\n",
    "* 5.人脸搜索\n",
    "* 6.人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "777d6b5b",
   "metadata": {},
   "source": [
    "## 创建FaceSet\n",
    "* 描述\n",
    "> 1.创建一个人脸的集合FaceSet,\n",
    "\n",
    "\n",
    "* 请求参数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "316e8b07",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "643c96ce",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-16-a47873318402>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-16-a47873318402>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    是否必选\t参数名\t类型\t参数说明\u001b[0m\n\u001b[1;37m        \t^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4dd59a39",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "079c3eda",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "FaceSet_creat_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'ac0YXaZmbh0HVE-zbgiFXJi9oxoNQoPL'\n",
    "API_sercret = 'Q3BtiKl8a8x5No1IgQCtmW-NYTXjEFPz'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = FaceSet_creat_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "29d53c8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "30cf7470",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 86,\n",
       " 'facesets': [{'faceset_token': 'cba7203510c3f27eef9076f3b4b7079b',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648467637,a497abd5-6d95-44dd-a86d-763b678bd4c1'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3c5ab70",
   "metadata": {},
   "source": [
    "# 查询FaceSet(人脸库)\n",
    "* 描述\n",
    "> 1.获取某一API Key 下的 FaceSet 列表及其 faceset_token、outer_id、display_name 和 tags 等信息。\n",
    "> 2.注意：2017年8月16日后，调用本接口将不会一次性返回全量的 FaceSet。单次查询最多返回 100 个 FaceSet。如需获取全量数据，需要配合使用 start 和 next 参数。请尽快修改调整您的程序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "299e6134",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "FaceSet_getfacesets_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'ac0YXaZmbh0HVE-zbgiFXJi9oxoNQoPL'\n",
    "API_sercret = 'Q3BtiKl8a8x5No1IgQCtmW-NYTXjEFPz'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = FaceSet_getfacesets_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "97434ae3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cba7203510c3f27eef9076f3b4b7079b'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['facesets'][-1]['faceset_token']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b86afd6",
   "metadata": {},
   "source": [
    "# 更新数据集的信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "c7a7f428",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "FaceSet_update_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'ac0YXaZmbh0HVE-zbgiFXJi9oxoNQoPL'\n",
    "API_sercret = 'Q3BtiKl8a8x5No1IgQCtmW-NYTXjEFPz'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'faceset_token':faceset_token,\n",
    "    'display_name':'广州南方学院1栋学生宿舍楼',\n",
    "    'new_outer_id':'NFUSD00',\n",
    "    'user_data':'6层,满员,女生宿舍,460位同学,均已录入face_token'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = FaceSet_update_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "9998ab20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 69,\n",
       " 'error_message': 'BAD_ARGUMENTS: user_data',\n",
       " 'request_id': '1648470833,b99f6e9b-b051-4d29-837f-12a7eafb3c7e'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "454529d6",
   "metadata": {},
   "source": [
    "# 检查数据集合信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "841b42fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "FaceSet_getdetail_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'ac0YXaZmbh0HVE-zbgiFXJi9oxoNQoPL'\n",
    "API_sercret = 'Q3BtiKl8a8x5No1IgQCtmW-NYTXjEFPz'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = FaceSet_getdetail_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "becc6439",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 69,\n",
       " 'error_message': 'BAD_ARGUMENTS: user_data',\n",
       " 'request_id': '1648470833,b99f6e9b-b051-4d29-837f-12a7eafb3c7e'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e482e159",
   "metadata": {},
   "source": [
    "# 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4ac6b219",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'detect'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-33-ec1dd0eed591>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mdetect\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'detect'"
     ]
    }
   ],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa34bf2c",
   "metadata": {},
   "source": [
    "---\n",
    "no moudle name is detect\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ca89ffe",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "16cdac5c",
   "metadata": {},
   "source": [
    "# 存入face_token数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0192d27b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 人脸检测\n",
    "import detect\n",
    "image_url = 'Liu_01.jpeg'\n",
    "detect.face_detect(API_KEY,API_S,image_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07005679",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_token_Liu = detect.face_detect(API_KEY,API_S,image_url)['faces'][0]['face_token']\n",
    "face_token_Liu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29853a7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "调用创建查询比对"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
